package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.orm.mybatis.MyBatisBaseServiceImpl;
import org.dnzx.framework.orm.mybatis.MybatisBaseDao;
import org.dnzx.framework.page.PageResult;
import org.dnzx.util.EmailUtil;
import org.springframework.stereotype.Service;

import com.aliyuncs.exceptions.ClientException;
import com.dnzx.dao.ISmsDao;
import com.dnzx.model.SmsBO;
import com.dnzx.service.ISmsService;

/**
 * @version 1.0
 */
@Service
public class SmsServiceImpl extends MyBatisBaseServiceImpl<SmsBO, Long>
		implements ISmsService {

	@Resource
    private ISmsDao smsDao;
    
    @Override
    public MybatisBaseDao<SmsBO, Long> getBaseDAO() {
        return this.smsDao;
    }

    @Override
    public void save(SmsBO smsBO) {
    	try {
//			AliSmsUtil.sendSms(smsBO.getMobile(), smsBO.getSignName(),
//					smsBO.getTemplateCode(), smsBO.getTemplateParam());
    		EmailUtil.send(smsBO.getMobile(), smsBO.getTemplateCode(), smsBO.getTemplateParam());
			this.smsDao.save(smsBO);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    @Override
    public void delete(Long id) {
        this.smsDao.delete(id);
    }

    @Override
    public void update(SmsBO smsBO) {
    }
    
    @Override
    public SmsBO get(Long id) {
    	SmsBO smsBO = this.smsDao.get(id);
    	return smsBO;
    }
    
    @Override
    public boolean checkCode(SmsBO smsBO) {
    	List<SmsBO> smsBOList = this.smsDao.queryList(smsBO);
    	if (smsBOList == null || smsBOList.size() <= 0) {
    		return false;
    	}
    	long nowTime = System.currentTimeMillis();
        long codeTime = smsBOList.get(0).getAddTime().getTime();
        if (nowTime > codeTime + 120000L) {// 120秒内有效
        	return false;
        }
    	return true;
    }
    
    @Override
    public List<SmsBO> queryList(SmsBO smsBO) {
    	return this.smsDao.queryList(smsBO);
    }

    @Override
    public PageResult<List<SmsBO>> queryPage(SmsBO smsBO, PageResult<List<SmsBO>> pageResult) {
        int totalRows = this.smsDao.queryCount(smsBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(smsBO.getCurrentPage());
        pageResult.setPageSize(smsBO.getPageSize());
		List<SmsBO> records = this.smsDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), smsBO);
		pageResult.setT(records);
		return pageResult;
    }
	
}
